package com.screwdriver.jso.client.converter;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayString;

import java.util.Date;
import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: jan kowalski
 * Date: 27.08.11
 * Time: 23:32
 * To change this template use File | Settings | File Templates.
 */
public class EnumConverter {

    public static String toJsoValue(Enum<?> e) {
        return e.name();
    }

    public static <T extends Enum<T>> T toJavaValue(String value, Class<T> enumClass) {
        if (value != null)
            return Enum.valueOf(enumClass, value);
        else
            return null;
    }

     public static JsArrayString toJsoValue(Enum<?>[] e) {
        if (e == null)
            return null;

        JsArrayString array = (JsArrayString) JavaScriptObject.createArray();
        for (Enum<?> b : e) {
            array.push(toJsoValue(b));
        }

        return array;
    }

    public static  <T extends Enum<T>> T[] toJavaValue(JsArrayString jsArrayString, Class<T> enumClass) {
        if (jsArrayString == null)
            return null;
        LinkedList<T> bigDecimals = new LinkedList<T>();
        for (int i = 0; i < jsArrayString.length(); i++) {
            bigDecimals.add(toJavaValue(jsArrayString.get(i), enumClass));
        }
        return bigDecimals.toArray(createArray(enumClass.getEnumConstants()[0]));
    }

    private static <T extends Enum<T>> T[] createArray(T...t) {
        return t;
    }

}
